package org.kfive.nova.leetcode;

import java.util.regex.Pattern;

/**
 * @author zhanwu.lzw
 * @version V1.0
 * @Project nova
 * @Package org.kfive.nova.leetcode
 * @Description TODO
 * @date 2017年11月27日 18:27
 * @ModificationHistory Who        When        What
 * --------        --------        ------------------------------
 */
public class StringToInteger {

    public Integer myAoti(String str){
        // 1. null or empty string
        if (str == null || str.length() == 0) return 0;

        // 2. whitespaces
        str = str.trim();

        // 3. +/- sign
        boolean positive = true;
        int i = 0;
        if (str.charAt(0) == '+') {
            i++;
        } else if (str.charAt(0) == '-') {
            positive = false;
            i++;
        }

        // 4. calculate real value
        double tmp = 0;
        for ( ; i < str.length(); i++) {
            int digit = str.charAt(i) - '0';

            if (digit < 0 || digit > 9) break;

            // 5. handle min & max
            if (positive) {
                tmp = 10*tmp + digit;
                if (tmp > Integer.MAX_VALUE) return Integer.MAX_VALUE;
            } else {
                tmp = 10*tmp - digit;
                if (tmp < Integer.MIN_VALUE) return Integer.MIN_VALUE;
            }
        }

        int ret = (int)tmp;
        return ret;
    }

    public static void main(String[] args) {
        StringToInteger stringToInteger = new StringToInteger();
        Integer result = stringToInteger.myAoti("1");
        System.out.println(result);
    }
}
